﻿using System;
using OpenQA.Selenium;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Threading;
using System.Linq;
using OpenQA.Selenium.Chrome;

namespace CloudStorageLight.Web.Tests
{
    [TestClass]
    public class FileTransferTest
    {
        IWebDriver driver;
        static string account;


        // クラス内で最初のテストを実行する前に、ClassInitialize を使用してコードを実行してください
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            account = "test001";
        }
        //
        // クラス内のテストをすべて実行したら、ClassCleanup を使用してコードを実行してください
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
        }

        [TestInitialize]
        public void TestSetUp()
        {
            driver = SubScenarioUtil.CreateWebDriver();
            SubScenarioUtil.Login(account, AppUtil.GetAppConfigValue("User4"), AppUtil.GetAppConfigValue("Password4"), false, driver);
            SubScenarioUtil.Navigate(driver, "f/", account);
        }

        [TestCleanup]
        public void TestCleanup()
        {
            driver.Close();
        }

        [TestMethod]
        public void ファイル送信()
        {
            var email = AppUtil.GetAppConfigValue("TestEmail");
            var pass = AppUtil.GetAppConfigValue("TestPassword");
            var titleString = "自動テスト" + Guid.NewGuid().ToString();
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "TEST.txt").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "重要.xlsx").Click();
            SubScenarioUtil.ClickMenuCommand(driver, "transfer-file-command");

            var to = SubScenarioUtil.TryFind(driver, By.Id("To"));
            to.SendKeys(email);
            var title = driver.FindElement(By.Id("Title"));
            title.SendKeys(titleString);
            var password = driver.FindElement(By.Id("Password"));
            password.SendKeys(pass);
            driver.FindElement(By.Id("sendButton")).Click();

            string guid = null;
            for (int i=0; i<10; i++)
            {
                guid = SubScenarioUtil.GetFileTransferGuid(account, titleString);
                if (guid != null) break;
                Thread.Sleep(1000);
            }
            SubScenarioUtil.WaitBusy(driver);
            SubScenarioUtil.Navigate(driver, "Account/Logoff/", account);
            SubScenarioUtil.Navigate(driver, "t/FileTransfer/" + guid, account);

            var mailAddress2 = SubScenarioUtil.TryFind(driver,By.Id("mailAddress"));
            var password2 = SubScenarioUtil.TryFind(driver, By.Id("password"));

            mailAddress2.SendKeys(email);
            password2.SendKeys(pass);
            driver.FindElement(By.Id("login")).Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "TEST.txt").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "重要.xlsx").Click();

            driver.FindElement(By.Id("download-command")).Click();

            SubScenarioUtil.ClearDownloadFile("downloadfiles.zip");

            driver.FindElement(By.Id("download-zip-file-transfer-command")).Click();
            Assert.IsTrue(SubScenarioUtil.CheckDownloadFile("downloadfiles.zip"));

        }

        [TestMethod]
        public void ファイル受信()
        {
            var email = AppUtil.GetAppConfigValue("TestEmail");
            var pass = AppUtil.GetAppConfigValue("TestPassword");
            var titleString = "自動テスト" + Guid.NewGuid().ToString();
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "TEST.txt").Click();
            SubScenarioUtil.ClickMenuCommand(driver, "transfer-file-command");

            var to = SubScenarioUtil.TryFind(driver, By.Id("To"));
            to.SendKeys(email);
            var title = driver.FindElement(By.Id("Title"));
            title.SendKeys(titleString);
            var fileReciveMode = driver.FindElement(By.Id("FileReciveMode"));
            fileReciveMode.FindElement(By.XPath("./option[2]")).Click();
            driver.FindElement(By.Id("sendButton")).Click();

            string guid = null;
            for (int i = 0; i < 10; i++)
            {
                guid = SubScenarioUtil.GetFileTransferGuid(account, titleString);
                Thread.Sleep(1000);
                if (guid != null) break;
            }
            SubScenarioUtil.WaitBusy(driver);
            SubScenarioUtil.Navigate(driver, "t/FileTransfer/" + guid, account);

            var filePath = "UnitTest.txt";
            SubScenarioUtil.UploadFile(driver, Path.GetFullPath(filePath));


            SubScenarioUtil.Navigate(driver, "t/FileTransfer/" + guid, account);
            var uploadedlist = driver.FindElements(By.XPath(".//span[@class='item-name']"));
            Assert.IsTrue(uploadedlist.Any(x => x.Text == filePath));
        }

    }
}
